// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import ResMgr from "../Managers/ResMgr";

const {ccclass, property} = cc._decorator;

@ccclass
export default class MonsterBullet extends cc.Component {

    private m_speed:number = 0;

    private blood_value:number = 0;

    

    onLoad () 
    {
        this.initViews();
    }

    start ()
    {
        this.blood_value = Math.floor(Math.random() * 10);
        this.m_speed = 10 + Math.floor(Math.random() * 50);
    }


    public initViews():void
    {
        var textureStr:any = Math.floor(1 + Math.random() * 9);
        console.log("textureStr-->", textureStr);
        var texture2d = ResMgr.Instance.getAsset("Bullet",textureStr + "");
        cc.log("texture2d===",texture2d);
        this.node.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame();
        this.node.getComponent(cc.Sprite).spriteFrame.setTexture(texture2d);
        this.node.setContentSize(cc.size(texture2d.width,texture2d.height));
        this.node.getComponent(cc.BoxCollider).size = cc.size(texture2d.width,texture2d.height);

    }

        
　　onCollisionEnter(other,self):void
    {
        // cc.log("onCollisionEneter>>>>>");
        // if(other)
        // {
        // }
        // cc.log();
        // cc.log(other);


        if(other.node.group == "bullet")
        {
            cc.log("子弹=====");
            this.blood_value -= 1;
            if(this.blood_value == 0)
            {
                this.node.destroy();
            }
        }


    }

    //=> 当两物体刚发生碰撞时被触发
　　onCollisionStay(other,self):void
    {

    }

    //=> 当两物体发生碰撞后，并且有交集的时候被触发
　　onCollisionExit(other,self):void
    {

    }




    update (dt) 
    {

        // var radian = cc.misc.degreesToRadians(this.node.angle + 90);
        var movedis = dt * this.m_speed;
        // var moveDir:any = {};
        // moveDir.x = Math.cos(radian) * movedis;
        // moveDir.y = Math.sin(radian) * movedis;
        var pos:any = new cc.Vec2(this.node.x + 0,this.node.y - movedis);
        this.node.setPosition(pos);
        
        if(this.node.y < (-cc.winSize.height-this.node.height/2))
        {
            this.node.destroy();
        }


    }
}
